package com.zondy.mapgis.android.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import com.zondy.mapgis.explorer.MapApplication;


import android.content.Context;

public class FileOperation
{
	public File file;

	public FileOperation(File file)
	{
		this.file = file;
	}

	public boolean FileContainStr(String inputString)
	{
		ArrayList<String> fileStrArrayList = new ArrayList<String>();
		String[] readString = readFrFile().split("\r\n");
		for (int i = 0; i < readString.length; i++)
		{
			fileStrArrayList.add(readString[i]);
			if (fileStrArrayList.contains(inputString))
			{
				return true;
			}
		}
		return false;
	}

	public String readFrFile()
	{
		try
		{
			byte[] buff = new byte[256];
			FileInputStream fis = new FileInputStream(file);
			int len = fis.read(buff);
			ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
			outputStream.write(buff, 0, len);
			return new String(outputStream.toByteArray());

		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}

	// 追加到文件尾
	public void write2file(String string)
	{
		String context = string + "\r\n";
		try
		{
			FileOutputStream fOutputStream = new FileOutputStream(file, true);// 设置为true，可以追加
			fOutputStream.write(context.getBytes());
			fOutputStream.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	public void clearFile(String string)
	{
		String context = string + "\r\n";
		try
		{
			FileOutputStream fos = new FileOutputStream(file, false);
			fos.write(context.getBytes());
			fos.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * 读文件的得到文件字节流
	 * 
	 * @param filePath
	 * @return
	 */
	public static byte[] readFileToBytes(String filePath)
	{
		byte[] bytes = null;
		try
		{
			BufferedInputStream bi = new BufferedInputStream(new FileInputStream(filePath));
			// 输入流转字节流
			bytes = new byte[bi.available()];
			bi.read(bytes);
			bi.close();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bytes;

	}

	/**
	 * 把字节流放到本地
	 * 
	 * @param bytes
	 * @param filePath
	 * @throws IOException
	 */
	public static void writeBytesFile(byte[] bytes, String filePath) throws IOException
	{
		try
		{
			BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream(filePath));
			bo.write(bytes);
			bo.flush();
			bo.close();
		}
		catch (FileNotFoundException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 读输入流得到文件字节流
	 * 
	 * @param filePath
	 * @return
	 */
	public static byte[] readInputStreamToBytes(InputStream in)
	{
		byte[] bytes = null;
		try
		{
			BufferedInputStream bi = new BufferedInputStream(in);
			// 输入流转字节流
			bytes = new byte[bi.available()];
			bi.read(bytes);
			bi.close();
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bytes;

	}

	/**
	 * 输入流转字节流
	 * */
	public static byte[] InputStreamToByte(InputStream is) throws IOException
	{
		ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int ch;
		while ((ch = is.read(buffer)) != -1)
		{
			bytestream.write(buffer, 0, ch);
		}
		byte data[] = bytestream.toByteArray();
		bytestream.close();
		return data;

	}
	
	// 按日期排序
	public static List<File> orderByDate(List<File> flieLst)
	{
		List<File> orderFileLst = new ArrayList<File>();
		File[] fs = new File[flieLst.size()];
		flieLst.toArray(fs);
		Arrays.sort(fs, new Comparator<File>()
		{
			public int compare(File f1, File f2)
			{
				long diff = f1.lastModified() - f2.lastModified();
				if (diff > 0)
					return 1;
				else if (diff == 0)
					return 0;
				else
					return -1;
			}

			public boolean equals(Object obj)
			{
				return true;
			}

		});
		for (int i = fs.length - 1; i > -1; i--)
		{
//			System.out.println(fs[i].getName());
//			System.out.println(new Date(fs[i].lastModified()));
			orderFileLst.add(fs[i]);
		}
		return orderFileLst;
	}

	/**
	 * 
	 * @param context 上下文
	 * @param assetName assets目录件内的一个图片文件的文件名称(支持路径)
	 * @param filePath 写入SDCARD路径
	 */
	public static void mapxFromAsset(Context context, String assetName)
	{
		try
		{
			String fileFolder = MapApplication.SystemLibraryPath + "Map";
			String filePath = MapApplication.SystemLibraryPath + "Map/"+assetName;
			
			InputStream localInputStream = context.getAssets().open(assetName);
			byte[] bytes = readInputStreamToBytes(localInputStream);
			
			if(bytes.length > 0)
			{
				File file = new File(fileFolder);
				if(!file.exists())
				{
					file.mkdirs();
				}
				
				writeBytesFile(bytes, filePath);
			}
			
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}

	}

	/**
	 * 打印文件
	 * 
	 * @param fileLst
	 */
	public static void printLst(List<File> fileLst)
	{
		System.out.println("******************");
		for (File file : fileLst)
		{
			System.out.println(file.getName());
			System.out.println(new Date(file.lastModified()));
		}
		System.out.println("================");
	}
}
